package com.aier.admin_new.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.common.AndroidTag;
import com.aier.admin_new.common.AndroidType;
import com.aier.admin_new.common.MessageBaiduYunSendTransmitter;
import com.aier.admin_new.entity.SchoolMasterEmail;
import com.aier.admin_new.entity.SchoolMasterEmailDetail;
import com.aier.admin_new.service.IChannelClient;
import com.aier.admin_new.service.ISchoolMasterEmailService;
import com.aier.common.common.CommonSession;
import com.aier.common.common.ICommonDAO;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.entity.Letter;
import com.aier.user.entity.LetterDetail;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;
import com.baidu.yun.channel.model.BindInfo;

/**
 * 校长信箱action
 * @author Wkk300
 */
@Scope("prototype")
@Component("schoolMasterEmailAction")
public class SchoolMasterEmailAction extends AdminNewBaseAction{
	
    @Resource(name="userService")
    private IUserService userService;
	@Resource(name = "schoolTeacherService")
	private ISchoolTeacherService schoolTeacherService;
    @Resource(name="schoolMasterEmailService")
    private ISchoolMasterEmailService schoolMasterEmailService;
    @Resource(name="channelClient")
    private IChannelClient channelClient;
    
    // 错误码
    private int s;
    // 发私信时输入的昵称
    private String nickname;
    // 信箱留言ID
    private Integer smeid;
       
    // 发信人的用户id
    private Long suid;
    // 收信人的用户id
    private Long ruid;
    // 私信的内容
    private String content;
    //学校ID
    private Integer sid ;
    //信箱列表
    private List<SchoolMasterEmail>  emailList;
    private Long uid;
    private int account;//未读留言条数
    
	@Override
	public String execute() throws Exception {
		return super.execute();
	}
	
	//留言校长信箱
	public String sendEmail(){
		s=0;
		User u =userService.searchBabyByUserid(suid);
		List<SchoolTeacher> sctList=schoolTeacherService.searchSchoolTeacherList(sid);
		if(sctList.size()==0)
			return SUCCESS;
		long semid =schoolMasterEmailService.insertEmail(suid, u.getNickname(),u.getHeadimg(), content, sid);
		
		for(SchoolTeacher st : sctList){
			short admin=st.getIsadmin();
			ruid=st.getUid();
			if( admin==(short)1  &  st !=null){
				s=1;
				com.aier.user.entity.User user=userService.getAppUid(ruid);
				
				MessageBaiduYunSendTransmitter mbysst = new MessageBaiduYunSendTransmitter(user, "校长信箱",  u.getNickname()+"给你留言#"+semid, AndroidType.SENDEMAIL, suid, 2);
				mbysst.sendServer();
			}
		}
		
		return SUCCESS;
	}
	
	//回复评论
	public String replyEmail(){		
		User u =userService.searchBabyByUserid(suid);
		User us =userService.searchBabyByUserid(ruid);
		if( us==null){
			s=0;
			return SUCCESS;
		} 
		schoolMasterEmailService.insertSchoolMasterEmailDetail(ruid, sid, content, smeid);
		s=1;
		//baidu
		 com.aier.user.entity.User user=userService.getAppUid(ruid);
		 MessageBaiduYunSendTransmitter mbysst = new MessageBaiduYunSendTransmitter(user, "信箱回复",  u.getNickname()+"回复了你#"+smeid, AndroidType.REPLYEMAIL, suid, 2);
		 mbysst.sendServer();
		return SUCCESS; 
	}
	
    /**
     * 获取校长信箱列表
     * 取信息要根据sid去取
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchEmail() {
    	List<SchoolMasterEmail> list = schoolMasterEmailService.searchEmail(sid);
    	
        if (null == list) {
        	s = 0;
        }
      
        //信箱详情
        List<SchoolMasterEmailDetail>  smeList;
        emailList = new ArrayList<SchoolMasterEmail>();
        for(SchoolMasterEmail sa :list){
        	smeList = schoolMasterEmailService.searchEmailDetail(sa.getSmeid(), sid);
        	account = schoolMasterEmailService.searchEmailCount(sid,sa.getSuid());
        	sa.setStats(account);
        	sa.setSmeList(smeList);
        	emailList.add(sa);
        }
        s = 1;
        return SUCCESS;
    }
    
    
    /**
     * 个人用户的多条留言
     * @return
     */
    public String searchEmailDetail(){
        //信箱详情
        List<SchoolMasterEmailDetail>  smeList;
        emailList = new ArrayList<SchoolMasterEmail>();
        List <SchoolMasterEmail> list = schoolMasterEmailService.searchEmail(sid, uid);
        for(SchoolMasterEmail sa :list ){
        	smeList = schoolMasterEmailService.searchEmailDetail(sa.getSmeid(), sid);
        	sa.setSmeList(smeList);
        	emailList.add(sa);
        }
        	s = 1;
    	return SUCCESS;
    }
   
    /**
     * 清空校长信箱
     * @return
     */
    public String deleteLetter() {
        s = schoolMasterEmailService.deleteEmail(smeid);
        return SUCCESS;
    }
    
	public int getS() {
		return s;
	}

	public void setS(int s) {
		this.s = s;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public Long getSuid() {
		return suid;
	}

	public void setSuid(Long suid) {
		this.suid = suid;
	}

	public Long getRuid() {
		return ruid;
	}

	public void setRuid(Long ruid) {
		this.ruid = ruid;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}

	public List getEmailList() {
		return emailList;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public void setSmeid(Integer smeid) {
		this.smeid = smeid;
	}
	
}
